// pages/shop_list/index.js
import {navTo,navToTab,onInput,onClick,showText} from '../../utils/reuse'
import{getCourseListConfirm,getCourseSubmit} from '../../api/index'
import {formatTime} from '../../utils/util'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import {baseURL} from '../../utils/request'
import drawQrcode from '../../utils/weapp.qrcode.esm.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    active: 0,
    page:1,
    pagesize:10,
    page_show:true,
    page_text:'加载中...'
  },

  submitFn(e){
    Dialog.confirm({
      title: '您正在确认预约，是否要继续吗？'
    })
      .then((res) => {
        const id = e.currentTarget.dataset.id
        var obj = {
          uid:wx.getStorageSync('uid'),
          point_id:id,
        }
        getCourseSubmit(obj).then(res=>{
          if(res.sta == 1){
            showText(res.msg,1)
            this.setData({
              page:1,
              pagesize:this.data.pagesize,
              page_show:true,
              list:[]
            })
            this.getCourseListFn()
          }else{
            showText('操作失败！',2)
          }
        })
      })
      .catch((res) => {
        console.log(res)
      });
  },

  getCourseListFn(){
    var obj = {
      uid:wx.getStorageSync('uid'),
      shop_id:wx.getStorageSync('shop_id'),
      status: this.data.active,
      page:this.data.page,
      pagesize:this.data.pagesize,
    }
    getCourseListConfirm(obj).then(res=>{
      if(res.sta == 1){
        if(res.msg.length <= 0){
          return this.setData({
            page_show:false,
            page_text:'数据加载完毕！'
          })
        }else if(res.msg.length < this.data.pagesize){
          this.setData({
            page_show:false,
            page_text:'数据加载完毕！'
          })
        }

        res.msg.forEach(item=>{
          var arr = []
          if(item.images){
            var arr1 = []
            arr = item.images.split(',')
            arr.forEach(item=>{
              arr1.push(baseURL + item)
            })
            item.images = arr1
          }
        })
        this.setData({
          list:[...this.data.list,...res.msg]
        })
      }else{
        if(res.msg.length <= 0){
          this.setData({
            page_show:false,
            page_text:'数据加载完毕！'
          })
        }else{
          showText('发生错误！',2)
        }
      }
    })
  },

  onChange(event) {
    this.setData({
      list:[],
      page:1,
      pagesize:this.data.pagesize,
      active:event.detail.name
    })
    this.getCourseListFn()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id == '1'){
      this.setData({
        active:Number(options.id)
      })
    }else{
      this.getCourseListFn()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      list:[],
      page:1,
      pagesize:this.data.pagesize,
    })
    this.getCourseListFn()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      page:this.data.page + 1,
      pagesize:this.data.pagesize
    })
    if(this.data.page_show){
      this.getCourseListFn()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})